package com.situ.day17;

import static org.junit.Assert.assertEquals;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class JDBCTest {
	@Test
	public void testQuery1() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			//1、记载驱动 Class.forName("");
			Class.forName("com.mysql.jdbc.Driver");
			//2、获得连接对象Connection
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java1807", "root", "root");
			//3、写sql语句
			String sql = "select id,name,age,gender from student;";
			//4、创建Statement
			statement = connection.createStatement();
			//5、执行sql语句
			//     更新类(更新了表里面数据)：delete/update/insert      executeUpdate()   返回值int:表示你影响的行数
			//    查询(没有改变表里面数据)：select                     executeQuery()     返回结果集：ResultSet
			resultSet = statement.executeQuery(sql);
			List<Student> list = new ArrayList<>();
			while (resultSet.next()) {
				Integer id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Integer age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	@Test
	public void testQuery2() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,name,age,gender from student;";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			List<Student> list = new ArrayList<>();
			while (resultSet.next()) {
				Integer id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Integer age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
	}

	@Test
	public void testUpdate() {
		Connection connection = null;
		Statement statement = null;
		try {
			//2、获得连接对象Connection
			connection = JDBCUtil.getConnection();
			//3、写sql语句
			//String sql = "update student set age=age+8 where id=1";
			//String sql = "insert into student(name,age,gender) values('yyyy',30,'男')";
			String name = "uuuuu";
			Integer age = 43;
			String gender = "男";
			String sql = "insert into student(name,age,gender) values('"+name+"',"+age+",'"+gender+"')";
			//String sql = "delete from student where id=10";
			//4、创建Statement
			statement = connection.createStatement();
			//5、执行sql语句
			//     更新类(更新了表里面数据)：delete/update/insert      executeUpdate()   返回值int:表示你影响的行数
			//     查询(没有改变表里面数据)：select                             executeQuery()     返回结果集：ResultSet
			int count = statement.executeUpdate(sql);
			System.out.println("count: " + count);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//6、关闭连接
			JDBCUtil.close(connection, statement, null);
		}
	}

	
}
